<?php

namespace App\Jobs;

use App\Extend\IPweb;
use App\Models\OrderInfo;
use App\Models\StaticProxyDetail;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

class StaticProxyRenewalJob extends Job
{
    protected $user_id;

    protected $order_nu;

    protected $is_renew;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user_id, $order_nu, $is_renew = false)
    {
        $this->user_id = $user_id;
        $this->order_nu = $order_nu;
        $this->is_renew = $is_renew;
    }

    /**
     * Execute the job.
     *
     * @return bool
     */
    public function handle()
    {
        Log::info('静态代理续期队列 user_id ' . $this->user_id . ' order_nu ' . $this->order_nu);
        $orderInfo = OrderInfo::where('user_id', $this->user_id)->where('order_nu', $this->order_nu)->where('status', 1)->first();
        if (empty($orderInfo)) {
            Log::error('静态代理续期队列 user_id ' . $this->user_id . ' order_nu ' . $this->order_nu . ' 订单不存在');
            return false;
        }

        $extend = json_decode($orderInfo->extend, true);
        if ($extend['days'] <= 30) {
            Log::error('静态代理续期队列 user_id ' . $this->user_id . ' order_nu ' . $this->order_nu . ' 天数不太于30天');
            return false;
        }
        if ($extend['days'] % 30 != 0) {
            Log::error("静态代理续期队列 天数不是30的整数倍 user_id:{$this->user_id} order_nu:{$this->order_nu}");
            return false;
        }
        $nums = ($extend['days'] / 30) - 1;
        Log::info('静态代理续期队列 user_id ' . $this->user_id . ' order_nu ' . $this->order_nu . ' 续费次数 ' . $nums);
        if ($nums <= 0) {
            Log::info("静态代理续期队列 无需续期（次数≤0） user_id:{$this->user_id} nums:{$nums}");
            return true;
        }

        if ($this->is_renew) {
            if (!isset($extend['ip'])) {
                Log::info("静态代理续期队列 续费ip不存在 user_id:{$this->user_id}");
                return true;
            }
            $ip = $extend['ip'];
            $staticProxyDetails = StaticProxyDetail::where('user_id', $this->user_id)->where('ip', $ip)->where('status', 1)->get();
        } else {
            $staticProxyDetails = StaticProxyDetail::where('user_id', $this->user_id)->where('order_nu', $this->order_nu)->where('status', 1)->get();
        }

        if (empty($staticProxyDetails)) {
            Log::error('静态代理续期队列 user_id ' . $this->user_id . ' order_nu ' . $this->order_nu . ' 代理记录不存在');
            return false;
        }

        $user = User::find($this->user_id);

        $IPweb = new IPweb();

        $target_expired_day = date('Y-m-d', strtotime('+' . $extend['days'] . ' days'));
        foreach ($staticProxyDetails as $staticProxyDetail) {
            $current_expired_day = date('Y-m-d', strtotime($staticProxyDetail->expired_time));
            if ($current_expired_day >= $target_expired_day) {
                Log::error('静态代理续期队列 user_id ' . $this->user_id . ' order_nu ' . $this->order_nu . ' 无需续期');
                continue;
            }
            for ($i = 0; $i < $nums; $i++) {
                if ($current_expired_day >= $target_expired_day) {
                    Log::error('静态代理续期队列 user_id ' . $this->user_id . ' order_nu ' . $this->order_nu . ' 无需续期2');
                    break;
                }
                $result = $IPweb->staticOrderRenew($staticProxyDetail->ip, 30, $user->customer_id);
                if ($result && $result['code'] == 200) {
                    $data = $result['data'];
                    $carbon = Carbon::parse($data['expired_at']);
                    $expired_time = $carbon->format('Y-m-d H:i:s');

                    $staticProxyDetail->expired_time = $expired_time;
                    $staticProxyDetail->status = 1;
                    $staticProxyDetail->save();

                    $current_expired_day = date('Y-m-d', strtotime($expired_time));
                } else {
                    Log::error('静态代理续期队列 user_id ' . $this->user_id . ' order_nu ' . $this->order_nu . ' 续费失败' . $result['message']);
                    $content = "*续期失败提醒*\n订单号：{$orderInfo->order_nu}";
                    dispatch(new SendNotificationJob($content));
                    break;
                }
            }
        }
        Log::error('静态代理续期队列 user_id ' . $this->user_id . ' order_nu ' . $this->order_nu . ' 续期完成');
        return true;
    }
}
